package com.open.common.task;

import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.open.loan.utils.HttpUtil;
import com.open.system.domain.LoanJuxinliPostInfoDO;

import java.util.HashMap;
import java.util.Map;

public class AlipayTest1 {

	public static String client_secret = "a0d730a9ebb4411f881b52f5e6d5361a";
	public static String access_token = "6aa02e8304cc4ed0abe34b111bf5354f";
	
	//10008	开始采集行为数据
	//10111	打开支付宝扫码进行授权--页面上渲染出二维码的同时需要调用获取二维码响应接口
	//10112	二维码已失效,请重新生成 ---需要再次调用提交支付宝采集请求接口
	//30000	错误信息
	
	//2.2.1提交申请表单接口
	public LoanJuxinliPostInfoDO AlipayApply(HashMap hashMap) {
		String url="https://www.juxinli.com/orgApi/rest/v1/alipay/applications/juying";
		HashMap<String, Object> paramMap = new HashMap<>();
		HashMap<String, Object> applyMap = new HashMap<>();
		HashMap<String, Object> basicMap = new HashMap<>();

		basicMap.put("name", String.valueOf(hashMap.get("name")));
		basicMap.put("cell_phone_num", String.valueOf(hashMap.get("cell_phone_num")));
		basicMap.put("id_card_num", String.valueOf(hashMap.get("id_card_num")));

		applyMap.put("basic_info",basicMap);
		paramMap.put("apply_info",applyMap);

		Console.log(JSONUtil.toJsonStr(paramMap));
        String res = HttpRequest.post(url)
                .body(JSONUtil.toJsonStr(paramMap))
                .execute().body();
		JSONObject jsonObject = JSONObject.parseObject(res);
		String content ="";
		LoanJuxinliPostInfoDO loanJuxinliPostInfoDO = new LoanJuxinliPostInfoDO();
		if("true".equals(jsonObject.getString("success"))){
			String token = String.valueOf(JSONUtil.parseObj(jsonObject.get("data").toString()).get("token"));


			loanJuxinliPostInfoDO.setToken(token);
			loanJuxinliPostInfoDO.setName(String.valueOf(hashMap.get("name")));
			loanJuxinliPostInfoDO.setCellPhoneNum(String.valueOf(hashMap.get("cell_phone_num")));
			loanJuxinliPostInfoDO.setIdCardNum(String.valueOf(hashMap.get("id_card_num")));
			//content= JSONObject.parseObject(jsonObject.get("data").toString()).get("content").toString();
		}
		return AlipayCaiji(loanJuxinliPostInfoDO);
	}
	
	//2.2.2提交支付宝采集请求接口
	public LoanJuxinliPostInfoDO AlipayCaiji(LoanJuxinliPostInfoDO loanJuxinliPostInfoDO) {

		String url="https://www.juxinli.com/orgApi/rest/v1/alipay/message/collect/req/juying";
		Map<String,String>map=new HashMap<String,String>();
		map.put("token", loanJuxinliPostInfoDO.getToken());
		String res = HttpRequest.post(url)
				.body(JSONUtil.toJsonStr(map))
				.execute().body();
		JSONObject jsonObject = JSONObject.parseObject(res);

		loanJuxinliPostInfoDO.setContet(String.valueOf(JSONUtil.parseObj(jsonObject.get("data").toString()).get("content")));
		loanJuxinliPostInfoDO.setCode(String.valueOf(JSONUtil.parseObj(jsonObject.get("data").toString()).get("process_code")));

		Console.log(jsonObject);
		return loanJuxinliPostInfoDO;
	}


	//2.2.3获取二维码响应接口
	public JSONObject GetCodeReturnInfo(String token) {
		String url="https://www.juxinli.com/orgApi/rest/v1/alipay/messages/qrcodeCollect/resp/";
		Map<String,String>map=new HashMap<String,String>();
		map.put("token",token);
		Object returnJson=HttpUtil.sendPostJsonData(url,JSON.toJSONString(map));
		System.out.println(returnJson);//字段2
		JSONObject jsonObject =JSONObject.parseObject(returnJson.toString());
		if("true".equals(jsonObject.getString("success"))){
			GetCodeReturnInfo(token);
			String data=jsonObject.getString("data");
            jsonObject = JSONObject.parseObject(data.toString());
        }
        return jsonObject;
	}
	
	
	//1.3.1根据采集TOKEN获取支付宝原始数据接口
    public static JSONObject GetAlipayInfoByToken(String token) {
		String url="https://www.juxinli.com/api/v1/access_alipay_raw_data_by_token";
		Map<String,String>map=new HashMap<String,String>();
		map.put("client_secret",client_secret);
		map.put("access_token",access_token);
		map.put("token","077b9e67fd3a40ac940c09bdddd9bc1d");
		String returnJson=HttpUtil.sendGet(url,map);
		System.out.println(returnJson);
		JSONObject jsonObject =JSONObject.parseObject(returnJson);
		String msg=jsonObject.getString("note");
		if("30019".equals(jsonObject.getString("code"))){
			System.out.println(msg);
			System.out.println(msg);
		}else if ("10001".equals(jsonObject.getString("code"))) {
			System.out.println(msg);
			System.out.println("参数client_secret不能为空");
		}else if ("10005".equals(jsonObject.getString("code"))) {
			System.out.println(msg);
			System.out.println("机构授权access_token不能为空");
		}else if ("10015".equals(jsonObject.getString("code"))) {
			System.out.println(msg);
			System.out.println("请输入采集流程token");
		}else if ("20002".equals(jsonObject.getString("code"))) {
			System.out.println(msg);
			System.out.println("机构不存在");
		}else if ("20003".equals(jsonObject.getString("code"))) {
			System.out.println(msg);
			System.out.println("账号已冻结，调用失败");
		}else if ("20007".equals(jsonObject.getString("code"))) {
			System.out.println(msg);
			System.out.println("身份验证失败");
		}else if ("20014".equals(jsonObject.getString("code"))) {
			System.out.println(msg);
			System.out.println("获取用户采集信息失败，请检查token是否正确");
		}else if ("20041".equals(jsonObject.getString("code"))) {
			System.out.println(msg);
			System.out.println("access_token已失效");
		}else if ("20048".equals(jsonObject.getString("code"))) {
			System.out.println(msg);
			System.out.println("获取支付宝原始数据失败");
		}
        return jsonObject;
	}


	public static void main(String[] args) {
		String a="1";
		GetAlipayInfoByToken(a);
	}
	
	
	
}
